package com.mcj.tokendemo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.mcj.tokendemo.entity.LoginUser;
import com.mcj.tokendemo.entity.User;
import com.mcj.tokendemo.mapper.MenuMapper;
import com.mcj.tokendemo.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
	
	@Autowired
	private UserMapper userMapper;
	
	@Autowired
	private MenuMapper menuMapper;
	
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		
		// 查询用户信息
		LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(User::getUserName, username);
		User user = userMapper.selectOne(queryWrapper);
		// 如果没有查询到对应用户，抛出异常
		if (Objects.isNull(user)) {
			throw new RuntimeException("账号或密码错误");
		}
		// 查询对应的权限信息
		List<String> list = menuMapper.selectPermsByUserId(user.getId());
		
		return new LoginUser(user, list);
	}
}
